// SPDX-FileCopyrightText: Copyright 2023 citrus Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "binder.h"
#include "common/assert.h"
#include "fs_android.h"
#include "common/string_util.h"

namespace Common::FS::Android {

bool IsContentUri(const std::string& path) {
    constexpr std::string_view prefix = "content://";
    if (path.size() < prefix.size()) [[unlikely]] {
        return false;
    }

    return path.find(prefix) == 0;
}

s32 OpenContentUri(const std::string& filepath, OpenMode openmode) {
    const char* mode = "";
    switch (openmode) {
    case OpenMode::Read:
        mode = "r";
        break;
    default:
        UNIMPLEMENTED();
        return -1;
    }

    int result = -1;
    Binder::getInstanceFrontend()->NativeLibrary_openContentUri(filepath, mode, reinterpret_cast<int64_t>(&result));
    return result;
}

u64 GetSize(const std::string& filepath) {
    int64_t res = 0;
    Binder::getInstanceFrontend()->NativeLibrary_getSize(filepath, &res);
    return static_cast<u64>(res);
}

bool IsDirectory(const std::string& filepath) {
    bool res = false;
    Binder::getInstanceFrontend()->NativeLibrary_isDirectory(filepath, &res);
    return res;
}

bool Exists(const std::string& filepath) {
    bool res = false;
    Binder::getInstanceFrontend()->NativeLibrary_exists(filepath, &res);
    return res;
}

std::string GetParentDirectory(const std::string& filepath) {
    std::string res;
    Binder::getInstanceFrontend()->NativeLibrary_getParentDirectory(filepath, &res);
    return res;
}

std::string GetFilename(const std::string& filepath) {
    std::string res;
    Binder::getInstanceFrontend()->NativeLibrary_getFilename(filepath, &res);
    return res;
}

} // namespace Common::FS::Android
